/*
 *  Copyright (C) 2010:
 *                 Buzzanca Marco <marco.bzn@gmail.com>
 *         Monaco Davide Giuseppe <black.ralkass@gmail.com>
 *              Petralia Veronica <veronicapetralia@gmail.com>
 *                    Tino Andrea <andry.tino@gmail.com>
 *
 * This file is part of DS2011BMP
 *
 * DS2011BMP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DS2011BMP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 *
 * NOTE: this file should not be included directly,
 *       Include synque.h instead.
 */

#include <boost/thread/condition_variable.hpp>
#include <boost/thread/mutex.hpp>
#include <deque>

/**
 * @brief Synchronized FIFO queue
 *
 * Synchronized queue is a container adaptor used in a FIFO context that provide a
 * concurrent behaviour avoiding producers and consumers to handle concurrency
 * explicitely. In order to do so, there are two differences between a STL queue
 * and a synque:
 *
 * 1. SIZE
 *    By default queues have not a maximum size.
 *    A %synque must know wether or not the container is supposed to have a maximum
 *    size, because in this case a container-full condition variable should be
 *    handled in order to make producers wait.
 *
 * 2. POP ELEMENTS
 *    The pop mechanism must include either the access to the next element and its
 *    removal from the container. The presence of more than one consumer could
 *    raise problems.
 *
 * It is possible to specify another type of container, but it must implement following
 * methods: push_back(), pop_front() and front();
 *
 */
template < typename _Tp, unsigned long SIZE = 0, typename _Sequence = std::deque<_Tp> >
class synque
{
public:
  typedef typename _Sequence::value_type        value_type;
  typedef typename _Sequence::reference         reference;
  typedef typename _Sequence::const_reference   const_reference;
  typedef typename _Sequence::size_type         size_type;
  typedef          _Sequence                    container_type;

protected:
  /**
   * Underlying container.
   */
  _Sequence                   __c;

  /**
   * Container-guarding mutex.
   */
  boost::mutex                __queue_m;

  /**
   * Empty container condition variable.
   */
  boost::condition_variable   __non_empty_c;

  /**
   * Full container condition variable.
   */
  boost::condition_variable   __non_full_c;

public:
  /**
   * Constructor.
   */
  explicit synque(const _Sequence& c = _Sequence())
    : __c(c) { }

  /**
   * Destructor.
   */
  ~synque() { };

  /**
   * @brief Returns true if container is empty.
   */
  bool empty(void)
  {
    boost::mutex::scoped_lock lock(__queue_m);

    return __c.empty();
  }

  /**
   * @brief Returns size.
   */
  size_type size(void)
  {
    boost::mutex::scoped_lock lock(__queue_m);

    return __c.size();
  }

  /**
   * @brief Insert element concurrently.
   */
  void push(const value_type& x)
  {
    boost::mutex::scoped_lock lock(__queue_m);

    // __non_full_c will be used only if SIZE is specified
    while (SIZE != 0 && __c.size() == SIZE)
      __non_full_c.wait(lock);

    __c.push_back(x);

    lock.unlock();

    __non_empty_c.notify_one();
  }

  /**
   * @brief Access next element and delete it concurrently.
   * It's necessary to perform the two actions 'atomically' to avoid
   * inconsistency.
   */
  value_type pop(void)
  {
    boost::mutex::scoped_lock lock(__queue_m);

    // consumers have to wait if container is empty
    while (__c.empty())
      __non_empty_c.wait(lock);

    // access next element and remove it, then return
    value_type el = __c.front();
    __c.pop_front();

    lock.unlock();

    if (SIZE != 0)
        __non_full_c.notify_one();

    return el;
  }

};
